package br.com.salao.ejb.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.com.salao.ejb.dao.FavoritosDao;
import br.com.salao.entity.Favoritos;

public class FavoritosDaoImpl implements FavoritosDao {

	private final GenericDao<Favoritos> dao;
	private EntityManager em;

	public FavoritosDaoImpl(EntityManager em){
		this.em = em;
		dao = new GenericDao<Favoritos>(em, Favoritos.class);		
	}

	public Favoritos incluir(Favoritos favoritos) throws Exception {
		return dao.salvar(favoritos);
	}

	@SuppressWarnings("unchecked")
	public List<Favoritos> buscarFavoritosPorLogin(int idLogin) throws Exception {
		Query query = em.createQuery("from Favoritos f where f.login.id = :idLogin");
		query.setParameter("idLogin", idLogin);
		return query.getResultList();
	}

	public Favoritos buscarFavoritosPorLoginEEstabelecimento(int idLogin, int idEstabelecimento) throws Exception {
		Query query = em.createQuery("from Favoritos f where " +
				"f.login.id = :idLogin and " +
		"f.estabelecimento.id = idEstabelecimento");
		query.setParameter("idLogin", idLogin);
		query.setParameter("idEstabelecimento", idEstabelecimento);	
		return (Favoritos) query.getSingleResult();
	}
	
	public void excluirFavorito(int idLogin, int idEstabelecimento) throws Exception{

		try{
			Query query = em.createQuery("delete from Favoritos f " +
					"where f.estabelecimento.id = :idEstabelecimento " +
			        "and f.login.id = :idLogin " );

			query.setParameter("idEstabelecimento", idEstabelecimento);	
			query.setParameter("idLogin", idLogin);		
			query.executeUpdate();

		}catch (Exception e) {			
			throw e;			
		}
	}
}


